package com.ruoyi.web.controller.consumerAdmin;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.consumer.controller.wallet.WalletService;
import com.ruoyi.consumer.domain.*;
import com.ruoyi.consumer.service.ICDealCompanyService;
import com.ruoyi.consumer.service.impl.CCompanyServiceImpl;
import com.ruoyi.consumer.service.impl.COrderInvoiceServiceImpl;
import com.ruoyi.consumer.service.impl.CUserServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;
import static com.ruoyi.common.utils.PageUtils.startPage;
import static com.ruoyi.consumer.utils.TableDataUtil.getDataTable;

/**
 * @author: lizhouwei
 * @ date : 2023/11/15
 * @description:
 */
@Api(tags = "管理员钱包管理")
@RestController
@RequestMapping("/api/wallet-com")
public class WalletCompanyController {
    @Autowired
    private ICDealCompanyService cDealCompanyService;

    @Autowired
    private CCompanyServiceImpl companyService;

    @Autowired
    private CUserServiceImpl cUserService;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private WalletService walletService;

    @Autowired
    private COrderInvoiceServiceImpl cOrderInvoiceService;


    public TableDataInfo getDataTable(List<CDealCompany> list, List<?> list1)
    {
        // 使用原始 list 创建 PageInfo 对象来保留分页信息
        PageInfo<CDealCompany> pageInfo = new PageInfo<>(list);
        TableDataInfo rspData = new TableDataInfo(list1, (int)pageInfo.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list1);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    /**
     * 查询公司钱包流水记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CDealCompany cDealCompany) {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        if (sysUser == null) {
            new RuntimeException("管理员不存在");
        }
        //查询当前管理员所处公司
        CCompany company = new CCompany();
        company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
        List<CCompany> cCompanies = companyService.selectCCompanyList(company);
        if (cCompanies.size() == 0) {
            new RuntimeException("管理员所处公司不存在");
        }

        cDealCompany.setCompanyId(cCompanies.get(0).getCompanyId());
        startPage();
        List<CDealCompany> list = cDealCompanyService.selectCDealCompanyList(cDealCompany);
        List<AjaxResult> ajaxResults = new ArrayList<>();
        for (CDealCompany dealCompany : list) {
            AjaxResult ajax = new AjaxResult();
            ajax.put("orderId",dealCompany.getDeviceOrderId());
            ajax.put("dealId", dealCompany.getDealRecordId());
            ajax.put("dealType", dealCompany.getDealType());
            ajax.put("dealAmount", dealCompany.getDealAmount());
            //检查是否已经开发票
            ajax.put("invoiceStatus", dealCompany.getInvoiceStatus());
            if (dealCompany.getInvoiceStatus() == 1) {
//                查询发票信息
                COrderInvoice invoice = new COrderInvoice();
                invoice.setOrderId(dealCompany.getDeviceOrderId());
                List<COrderInvoice> cOrderInvoices = cOrderInvoiceService.selectCOrderInvoiceList(invoice);
                if (cOrderInvoices.size() == 0) {
                    new RuntimeException("发票信息不存在");
                } else {
                    ajax.put("invoiceId", cOrderInvoices.get(0).getInvoiceId());
                    ajax.put("invoiceType", cOrderInvoices.get(0).getInvoiceType());
                    ajax.put("invoiceFile", cOrderInvoices.get(0).getInvoiceFile());
                    ajax.put("status", cOrderInvoices.get(0).getStatus());
                }
            }
            ajax.put("createTime", DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, dealCompany.getCreateTime()));
            //查询用户信息
            CUser cUser = cUserService.selectCUserByUserId(dealCompany.getTargetUserId());
            if (cUser != null) {
                ajax.put("userName", cUser.getUserName());
                ajax.put("userPhone", cUser.getUserPhone());
            }
            //查询充值信息
            COrder cOrder = new COrder();
            cOrder.setOrderId(dealCompany.getDeviceOrderId());
            ajaxResults.add(ajax);
        }
        return getDataTable(list,ajaxResults);
    }

    @ApiOperation("公司钱包充值")
    @PutMapping
    public AjaxResult companyRecharge(@RequestBody CWalletCompany cWalletCompany) {
        System.out.println("公司钱包充值" + cWalletCompany.getWalletFree());

        //更新钱包操作
        walletService.rechargeCompanyWallet(cWalletCompany.getWalletPersonId(), cWalletCompany.getWalletFree());
        return null;
    }

    //更改钱包流水发票状态
    @ApiOperation("更改钱包流水发票状态")
    @PostMapping("/update-invoice-status")
    public AjaxResult updateInvoiceStatus(@ApiParam Integer dealId) {
        CDealCompany cDealCompany = new CDealCompany();
        cDealCompany.setDealRecordId(dealId);
        cDealCompany.setInvoiceStatus(1);
        return AjaxResult.success(cDealCompanyService.updateCDealCompany(cDealCompany));
    }


}
